﻿Public Class FractionManipulator
	Private _numer As Int32
	Private _denom As Int32

	Public Sub New(ByVal numer As Int32, ByVal denom As Int32)
		_numer = numer
		_denom = denom
	End Sub
	''' <summary>
	''' Converts the signed numerator and denominator values to its equivalent string representation. A fraction (x/y) is returned when applicable.
	''' </summary>
	''' <returns></returns>
	''' <remarks></remarks>
	Public Overrides Function ToString() As String
		Dim numer As Int32 = _numer
		Dim denom As Int32
		Dim result As String

		If _denom = 0 Then
			denom = 1
		Else
			denom = _denom
		End If

		'Make the numerator "store" the sign
		If (denom < 0) Then
			numer = numer * -1
			denom = denom * -1
		End If

		Reduce(numer, denom)

		If (numer = 0) Then
			result = "0"
		ElseIf (denom = 1) Then
			result = numer & ""
		Else
			result = numer & "/" & denom
		End If

		Return result
	End Function
	''' <summary>
	''' Reduces the rational number by dividing both the numerator and the denominator by their greatest common divisor.
	''' </summary>
	''' <param name="numer"></param>
	''' <param name="denom"></param>
	''' <remarks></remarks>
	Private Sub Reduce(ByRef numer As Int32, ByRef denom As Int32)
		If (numer <> 0) Then
			Dim common As Int32 = GCD(Math.Abs(numer), denom)

			numer = numer / common
			denom = denom / common
		End If
	End Sub
	''' <summary>
	''' Computes and returns the greatest common divisor of the two positive parameters. Uses Euclid's algorithm.
	''' </summary>
	''' <param name="num1"></param>
	''' <param name="num2"></param>
	''' <returns></returns>
	''' <remarks></remarks>
	Private Function GCD(ByVal num1 As Int32, ByVal num2 As Int32) As Int32
		Do While (num1 <> num2)
			If (num1 > num2) Then
				num1 = num1 - num2
			Else
				num2 = num2 - num1
			End If
		Loop

		Return num1
	End Function
End Class
Public Class UFraction
	Private _numer As UInt32
	Private _denom As UInt32

	Public Sub New(ByVal numer As UInt32, ByVal denom As UInt32)
		_numer = numer
		_denom = denom
	End Sub
	''' <summary>
	''' Converts the unsigned numerator and denominator values to its equivalent string representation. A fraction is used when applicable.
	''' </summary>
	''' <returns></returns>
	''' <remarks></remarks>
	Public Overrides Function ToString() As String
		Dim numer As UInt32 = _numer
		Dim denom As UInt32
		Dim result As String

		If _denom = 0 Then
			denom = 1
		Else
			denom = _denom
		End If

		Reduce(numer, denom)

		If numer = 0 Then
			result = "0"
		ElseIf (denom = 1) Then
			result = numer.ToString & ""
		Else
			result = numer.ToString & "/" & denom
		End If
		Return result
	End Function
	''' <summary>
	''' Reduces the rational number by dividing both the numerator and the denominator by their greatest common divisor.
	''' </summary>
	''' <param name="numer"></param>
	''' <param name="denom"></param>
	''' <remarks></remarks>
	Public Sub Reduce(ByRef numer As UInt32, ByRef denom As UInt32)
		If (numer <> 0) Then
			Dim Common As UInt32 = GCD(numer, denom)

			numer = numer / Common
			denom = denom / Common
		End If
	End Sub
	''' <summary>
	''' Computes and returns the greatest common divisor of the two positive parameters. Uses Euclid's algorithm.
	''' </summary>
	''' <param name="num1"></param>
	''' <param name="num2"></param>
	''' <returns></returns>
	''' <remarks></remarks>
	Function GCD(ByVal num1 As UInt32, ByVal num2 As UInt32) As UInt32
		Do While (num1 <> num2)
			If (num1 > num2) Then
				num1 = num1 - num2
			Else
				num2 = num2 - num1
			End If
		Loop
		Return num1
	End Function
End Class
